<?php
/**
 * Created by PhpStorm.
 * User: qingyun
 * Date: 17/4/27
 * Time: 下午8:52
 */
namespace Common\Model;
use Think\Crypt\Driver\Think;
use Think\Model;

class MenuModel extends Model{

    //无表前缀的菜单表名
    protected $table;
    //有前缀的菜单表
    protected $menuTable;
    //表前缀
    protected $prefix;

    /**构造函数初始化数据
     * @param string $adminTable
     * @param string $prefix
     */
    public function __construct($menuTable,$prefix = ''){
        parent::__construct();
        $this->prefix = empty($prefix) ? C('DB_PREFIX') : $prefix;
        $this->table = $menuTable;
        $this->menuTable = $this->prefix.$menuTable;
    }

    /**
     * @param $data 添加菜单，把添加信息传进来
     * @return array|bool
     */

    public function addMenu($data){
        $check_info = $this->checkMenuInfo($data);
        if($check_info !== true){
            return $check_info;
        }
        $menuData = $this->formatMenuInfo($data);
        $menu = M($this->table);
        $id = $menu->add($menuData,[],true);
        if($id){
            return $this->returnInfo('','成功',0);
        }else{
            return $this->returnInfo('','失败');
        }
    }

    /**
     * @param $id 有id的话，就是修改，把这一条数据返回，没有id就是列表，把所有数据都返回
     * @param string $field 查询的字段
     * @param string $order 排序
     * @return array
     */

    public function menuList($id,$field="id",$order="sort desc"){
        $menu = M($this->table);
        $list = $menu->field($field);
        if(!empty($id)){
            $where = ['id' => $id];
            $list = $list->where($where);
        }
        $list = $list->order($order)->select();
        if(empty($list)){
            return $this->returnInfo('','没有找到');
        }else{
            return $this->returnInfo($list,'成功',0);
        }
    }

    /**
     * @param $id 把删除项的id传进来
     * @return array
     */

    public function deleteMenu($id){
        if(empty($id) || ($id == 1)){
            $this->returnInfo('','删除异常');
        }
        $menu = M($this->table);
        if(!$menu->where(['id' =>$id])->delete()){
            return $this->returnInfo('','失败');
        }
        return $this->returnInfo('','成功',0);
    }

    /**
     * @param $data 把传递过来的数据处理一下，重新赋给新的变量名
     * @return array 把值存到一个数组里返回
     */

    public function formatMenuInfo($data){
        $id = empty($data['id']) ? 0 : $data['id'];
        $sort = empty($data['sort']) ? 0 : $data['sort'];
        $pid = empty($data['pid']) ? 0 : $data['pid'];
        $status = empty($data['status']) ? 0 : $data['status'];
        $type = empty($data['type']) ? 1 : $data['type'];
        $datas = ['id' => $id,'title' => $data['title'],'link' => $data['link'],'sort' => $sort,'pid' => $pid,'status' => $status,'type' => $type];
        return $datas;
    }

    /**
     * @return array|bool 判断传进来的表单数据是否正常，正常返回TRUE
     */

    public function checkMenuInfo($data){
        if(empty($data['title'])){
            return $this->returnInfo('','菜单名字不能为空');
        }
        if(empty($data['link'])){
            return $this->returnInfo('','链接不能为空');
        }
        $menu = M($this->table)->field('id')->where(['title' => $data['title'],'link' => $data['link']])->count();
        if($menu > 0){
            return $this->returnInfo('','菜单不能重复');
        }
        return true;
    }

    /**
     * 改变记录状态
     * @param $id
     * @return array
     */
    public function changeMode($id){
        if(empty($id) || ($id ==1 )){
            $this->returnInfo('','修改异常');
        }
        $menu = M($this->table);
        $menuInfo = $menu->field('status')->where(['id' => $id])->find();
        if(empty($menuInfo['status'])){
            $menu->where(['id' => $id])->setField('status',1);
            $result = $this->returnInfo(1,'已开启',0);
        }else{
            $menu->where(['id' => $id])->setField('status',0);
            $result = $this->returnInfo(0,'已开启',0);
        }
        return $result;
    }

    /**
     * @param $data
     * @param string $msg 错误信息
     * @param int $status 成功为0，错误为1
     * @return array
     */
    public function returnInfo($data,$msg="",$status=1){
        return ['status' => $status,'msg' => $msg,'data' => $data];
    }

    public function getNavList(){
        $data = M($this->table)->field('id,pid,title,link')->select();
        $auth = new \Think\Auth();
        foreach($data as $key => $val){
            if(!$auth->check($val['link'],session('user.uid'))){
                unset($data[$key]);
            }
            return make_tree($data);
        }
    }
}